package com.hong.utils.executors;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Auther: Administrator
 * @Date: 2018/7/26 20:38
 * @Description:
 */
public class ExecutorsExample2 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newWorkStealingPool();
        List<Callable<String>> callableList = IntStream.range(0, 20).boxed().map(i ->
                (Callable<String>) () -> {
                    System.out.println("Thread "+Thread.currentThread().getName());
                    sleep(2);
                    return "Task-" + i;
                }
        ).collect(Collectors.toList());
        executorService.invokeAll(callableList).stream().map(future->{
            try {
                return future.get();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }).forEach(System.out::println);



        /*查看电脑的进程数*/
//        Optional.of(Runtime.getRuntime().availableProcessors())
//                .ifPresent(System.out::println);

    }
    /**休眠*/
    private static void sleep(long seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
